package com.sc.nft.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sc.nft.dao.DigitalCollectionDao;
import com.sc.nft.dao.OrderGasShareConfigDao;
import com.sc.nft.entity.DigitalCollection;
import com.sc.nft.entity.OrderGasShareConfig;
import com.sc.nft.entity.vo.OrderGasShareConfigVO;
import com.sc.nft.exception.GlobalRunTimeException;
import com.sc.nft.service.OrderGasShareConfigService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Objects;

/**
 * @author 刘国航
 * @desc
 * @since 2024/10/21 11:24
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class OrderGasShareConfigServiceImpl implements OrderGasShareConfigService {

    private final OrderGasShareConfigDao orderGasShareConfigDao;
    private final DigitalCollectionDao digitalCollectionDao;


    @Override
    public void createShareConfig(String shareName, Long collectionId, BigDecimal shareRatio) {
        Assert.isTrue(shareRatio.compareTo(BigDecimal.ZERO) > 0, () -> new GlobalRunTimeException("分红比例必须大于0"));
        Assert.isTrue(shareRatio.compareTo(new BigDecimal(100)) <= 0, () -> new GlobalRunTimeException("分红比例不能大于100%"));
        Assert.notNull(digitalCollectionDao.getById(collectionId), () -> new GlobalRunTimeException("藏品不存在"));
        Assert.isTrue(CollectionUtil.isEmpty(orderGasShareConfigDao.listByCollectionId(collectionId)), () -> new GlobalRunTimeException("该藏品已配置分红"));
        OrderGasShareConfig orderGasShareConfig = new OrderGasShareConfig();
        orderGasShareConfig.setShareName(shareName);
        orderGasShareConfig.setCollectionId(collectionId);
        orderGasShareConfig.setShareRatio(shareRatio);
        orderGasShareConfig.setStatus(Boolean.FALSE);
        orderGasShareConfig.insert();
    }

    @Override
    public void updateShareConfig(Long id, String shareName, BigDecimal shareRatio) {
        Assert.isTrue(shareRatio.compareTo(BigDecimal.ZERO) > 0, () -> new GlobalRunTimeException("分红比例必须大于0"));
        Assert.isTrue(shareRatio.compareTo(new BigDecimal(100)) <= 0, () -> new GlobalRunTimeException("分红比例不能大于100%"));
        OrderGasShareConfig orderGasShareConfig = orderGasShareConfigDao.getById(id);
        Assert.notNull(orderGasShareConfig, () -> new GlobalRunTimeException("分红配置不存在"));
        Assert.isFalse(orderGasShareConfig.getStatus(), () -> new GlobalRunTimeException("分红配置启用中，不能修改"));
        orderGasShareConfig.setShareName(shareName);
        orderGasShareConfig.setShareRatio(shareRatio);
        orderGasShareConfig.updateById();
    }

    @Override
    public void updateStatus(Long id, Boolean status) {
        OrderGasShareConfig orderGasShareConfig = orderGasShareConfigDao.getById(id);
        Assert.notNull(orderGasShareConfig, () -> new GlobalRunTimeException("分红配置不存在"));
        if (status) {
            Assert.isFalse(orderGasShareConfig.getStatus(), () -> new GlobalRunTimeException("已启用，不能重复启用"));
            List<OrderGasShareConfig> orderGasShareConfigList = orderGasShareConfigDao.listByStatus(Boolean.TRUE);
            BigDecimal enabledShareRatio = CollectionUtil.isNotEmpty(orderGasShareConfigList) ? orderGasShareConfigList.stream().map(OrderGasShareConfig::getShareRatio).reduce(BigDecimal.ZERO, BigDecimal::add) : BigDecimal.ZERO;
            Assert.isTrue((enabledShareRatio.add(orderGasShareConfig.getShareRatio())).compareTo(new BigDecimal(100)) <= 0, () -> new GlobalRunTimeException("分红比例总和不得大于100%"));
        } else {
            Assert.isTrue(orderGasShareConfig.getStatus(), () -> new GlobalRunTimeException("已禁用，不能重复禁用"));
        }
        orderGasShareConfig.setStatus(status);
        orderGasShareConfig.updateById();
    }

    @Override
    public IPage<OrderGasShareConfigVO> page(Page page, String shareName, Long collectionId) {
        return orderGasShareConfigDao.page(page, shareName, collectionId).convert(v -> {
            OrderGasShareConfigVO orderGasShareConfigVO = new OrderGasShareConfigVO();
            orderGasShareConfigVO.setId(v.getId());
            orderGasShareConfigVO.setShareName(v.getShareName());
            orderGasShareConfigVO.setCollectionId(v.getCollectionId());
            DigitalCollection collection = digitalCollectionDao.getById(v.getCollectionId());
            orderGasShareConfigVO.setCollectionName(Objects.nonNull(collection) ? collection.getFirstTitle() : "");
            orderGasShareConfigVO.setShareRatio(v.getShareRatio());
            orderGasShareConfigVO.setShareRatioName(v.getShareRatio() + "%");
            orderGasShareConfigVO.setStatus(v.getStatus());
            return orderGasShareConfigVO;
        });
    }

    @Override
    public OrderGasShareConfigVO detail(Long id) {
        OrderGasShareConfig orderGasShareConfig = orderGasShareConfigDao.getById(id);
        Assert.notNull(orderGasShareConfig, () -> new GlobalRunTimeException("配置不存在"));
        OrderGasShareConfigVO orderGasShareConfigVO = new OrderGasShareConfigVO();
        orderGasShareConfigVO.setId(orderGasShareConfig.getId());
        orderGasShareConfigVO.setShareName(orderGasShareConfig.getShareName());
        orderGasShareConfigVO.setCollectionId(orderGasShareConfig.getCollectionId());
        DigitalCollection collection = digitalCollectionDao.getById(orderGasShareConfig.getCollectionId());
        orderGasShareConfigVO.setCollectionName(Objects.nonNull(collection) ? collection.getFirstTitle() : "");
        orderGasShareConfigVO.setShareRatio(orderGasShareConfig.getShareRatio());
        orderGasShareConfigVO.setShareRatioName(orderGasShareConfig.getShareRatio() + "%");
        orderGasShareConfigVO.setStatus(orderGasShareConfig.getStatus());
        return orderGasShareConfigVO;
    }

}
